package com.example.administrator.demoa;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    //创建一个List来存放Fragment
    private List<Fragment> fragmentList = new ArrayList<>();
    private NewsFragmentAdapter adapter;
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;
    private TextView tv5;
    private TextView tv6;
    private TextView tv7;
    private TextView tv8;
    private TextView tv9;
    private TextView tv10;
    private TextView tv_user;
    private TextView tv_like;
    private CircleImageView profile_image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFragmentList();
        initViews();
        initViewPager();

        //初始化左侧滑动菜单
        initLeftDraw();
    }

    private void initLeftDraw() {
        tv_user = findViewById(R.id.tv_user);
        tv_user.setText(BmobUser.getCurrentUser(BmobUser.class).getUsername());
        tv_like = findViewById(R.id.tv_like);
        tv_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //登录可查看收藏，不然进入登录页面
                if(BmobUser.getCurrentUser(BmobUser.class) == null){
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent);
                    return;
                }

                Intent intent = new Intent(MainActivity.this, LikeActivity.class);
                startActivity(intent);
            }
        });

        profile_image = findViewById(R.id.profile_image);
        //打开手机系统的相册
        profile_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 3001);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case 3001:
                try {
                    Uri selectedImage = data.getData(); //获取系统返回的照片的Uri
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(selectedImage,
                            filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String path = cursor.getString(columnIndex);  //获取照片路径
                    cursor.close();

                    Bitmap bitmap = BitmapFactory.decodeFile(path);
                    profile_image.setImageBitmap(bitmap);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }


    private void initViews() {
        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        tv3 = findViewById(R.id.tv3);
        tv4 = findViewById(R.id.tv4);
        tv5 = findViewById(R.id.tv5);
        tv6 = findViewById(R.id.tv6);
        tv7 = findViewById(R.id.tv7);
        tv8 = findViewById(R.id.tv8);
        tv9 = findViewById(R.id.tv9);
        tv10 = findViewById(R.id.tv10);
        tv1.setBackgroundColor(Color.RED);

        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(0);
            }
        });
        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(1);
            }
        });
        tv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(2);
            }
        });
        tv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(3);
            }
        });
        tv5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(4);
            }
        });
        tv6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(5);
            }
        });
        tv7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(6);
            }
        });
        tv8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(7);
            }
        });
        tv9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(8);
            }
        });
        tv10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(9);
            }
        });

    }

    private void initFragmentList() {
        NewsFragment fragment1 = new NewsFragment();
        fragment1.setType("top");
        NewsFragment fragment2 = new NewsFragment();
        fragment2.setType("shehui");
        NewsFragment fragment3 = new NewsFragment();
        fragment3.setType("keji");
        NewsFragment fragment4 = new NewsFragment();
        fragment4.setType("tiyu");
        NewsFragment fragment5 = new NewsFragment();
        fragment5.setType("caijing");
        NewsFragment fragment6 = new NewsFragment();
        fragment6.setType("yule");
        NewsFragment fragment7 = new NewsFragment();
        fragment7.setType("guonei");
        NewsFragment fragment8 = new NewsFragment();
        fragment8.setType("guowai");
        NewsFragment fragment9 = new NewsFragment();
        fragment9.setType("junshi");
        NewsFragment fragment10 = new NewsFragment();
        fragment10.setType("shishang");
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);
        fragmentList.add(fragment4);
        fragmentList.add(fragment5);
        fragmentList.add(fragment6);
        fragmentList.add(fragment7);
        fragmentList.add(fragment8);
        fragmentList.add(fragment9);
        fragmentList.add(fragment10);
    }

    private void initViewPager() {
        viewPager = findViewById(R.id.view_pager);
        //由于viewPager中的缓存数为2，log日志请求一次，滑动fragment，会刷新，
        // 所以可以设置setOffscreenPageLimit()
        //数值不宜过大
        viewPager.setOffscreenPageLimit(8);
        adapter = new NewsFragmentAdapter(getSupportFragmentManager(), fragmentList);
        //使用适配器将viewPager与fragment页面结合
        viewPager.setAdapter(adapter);

        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);

                setViewColor(position);
            }
        });
    }

    private void setViewColor(int position) {
        switch (position){
            case 0:
                tv1.setBackgroundColor(Color.RED);
                tv2.setBackgroundColor(Color.TRANSPARENT);
                tv3.setBackgroundColor(Color.TRANSPARENT);
                tv4.setBackgroundColor(Color.TRANSPARENT);
                tv5.setBackgroundColor(Color.TRANSPARENT);
                tv6.setBackgroundColor(Color.TRANSPARENT);
                tv7.setBackgroundColor(Color.TRANSPARENT);
                tv8.setBackgroundColor(Color.TRANSPARENT);
                tv9.setBackgroundColor(Color.TRANSPARENT);
                tv10.setBackgroundColor(Color.TRANSPARENT);
                break;
            case 1:
                tv1.setBackgroundColor(Color.TRANSPARENT);
                tv2.setBackgroundColor(Color.RED);
                tv3.setBackgroundColor(Color.TRANSPARENT);
                tv4.setBackgroundColor(Color.TRANSPARENT);
                tv5.setBackgroundColor(Color.TRANSPARENT);
                tv6.setBackgroundColor(Color.TRANSPARENT);
                tv7.setBackgroundColor(Color.TRANSPARENT);
                tv8.setBackgroundColor(Color.TRANSPARENT);
                tv9.setBackgroundColor(Color.TRANSPARENT);
                tv10.setBackgroundColor(Color.TRANSPARENT);
                break;
            case 2:
                tv1.setBackgroundColor(Color.TRANSPARENT);
                tv2.setBackgroundColor(Color.TRANSPARENT);
                tv3.setBackgroundColor(Color.RED);
                tv4.setBackgroundColor(Color.TRANSPARENT);
                tv5.setBackgroundColor(Color.TRANSPARENT);
                tv6.setBackgroundColor(Color.TRANSPARENT);
                tv7.setBackgroundColor(Color.TRANSPARENT);
                tv8.setBackgroundColor(Color.TRANSPARENT);
                tv9.setBackgroundColor(Color.TRANSPARENT);
                tv10.setBackgroundColor(Color.TRANSPARENT);
                break;
            case 3:
                tv1.setBackgroundColor(Color.TRANSPARENT);
                tv2.setBackgroundColor(Color.TRANSPARENT);
                tv3.setBackgroundColor(Color.TRANSPARENT);
                tv4.setBackgroundColor(Color.RED);
                tv5.setBackgroundColor(Color.TRANSPARENT);
                tv6.setBackgroundColor(Color.TRANSPARENT);
                tv7.setBackgroundColor(Color.TRANSPARENT);
                tv8.setBackgroundColor(Color.TRANSPARENT);
                tv9.setBackgroundColor(Color.TRANSPARENT);
                tv10.setBackgroundColor(Color.TRANSPARENT);
                break;
            case 4:
                tv1.setBackgroundColor(Color.TRANSPARENT);
                tv2.setBackgroundColor(Color.TRANSPARENT);
                tv3.setBackgroundColor(Color.TRANSPARENT);
                tv4.setBackgroundColor(Color.TRANSPARENT);
                tv5.setBackgroundColor(Color.RED);
                tv6.setBackgroundColor(Color.TRANSPARENT);
                tv7.setBackgroundColor(Color.TRANSPARENT);
                tv8.setBackgroundColor(Color.TRANSPARENT);
                tv9.setBackgroundColor(Color.TRANSPARENT);
                tv10.setBackgroundColor(Color.TRANSPARENT);
                break;
            case 5:
                tv1.setBackgroundColor(Color.TRANSPARENT);
                tv2.setBackgroundColor(Color.TRANSPARENT);
                tv3.setBackgroundColor(Color.TRANSPARENT);
                tv4.setBackgroundColor(Color.TRANSPARENT);
                tv5.setBackgroundColor(Color.TRANSPARENT);
                tv6.setBackgroundColor(Color.RED);
                tv7.setBackgroundColor(Color.TRANSPARENT);
                tv8.setBackgroundColor(Color.TRANSPARENT);
                tv9.setBackgroundColor(Color.TRANSPARENT);
                tv10.setBackgroundColor(Color.TRANSPARENT);
                break;
            case 6:
                tv1.setBackgroundColor(Color.TRANSPARENT);
                tv2.setBackgroundColor(Color.TRANSPARENT);
                tv3.setBackgroundColor(Color.TRANSPARENT);
                tv4.setBackgroundColor(Color.TRANSPARENT);
                tv5.setBackgroundColor(Color.TRANSPARENT);
                tv6.setBackgroundColor(Color.TRANSPARENT);
                tv7.setBackgroundColor(Color.RED);
                tv8.setBackgroundColor(Color.TRANSPARENT);
                tv9.setBackgroundColor(Color.TRANSPARENT);
                tv10.setBackgroundColor(Color.TRANSPARENT);
                break;
            case 7:
                tv1.setBackgroundColor(Color.TRANSPARENT);
                tv2.setBackgroundColor(Color.TRANSPARENT);
                tv3.setBackgroundColor(Color.TRANSPARENT);
                tv4.setBackgroundColor(Color.TRANSPARENT);
                tv5.setBackgroundColor(Color.TRANSPARENT);
                tv6.setBackgroundColor(Color.TRANSPARENT);
                tv7.setBackgroundColor(Color.TRANSPARENT);
                tv8.setBackgroundColor(Color.RED);
                tv9.setBackgroundColor(Color.TRANSPARENT);
                tv10.setBackgroundColor(Color.TRANSPARENT);
                break;
            case 8:
                tv1.setBackgroundColor(Color.TRANSPARENT);
                tv2.setBackgroundColor(Color.TRANSPARENT);
                tv3.setBackgroundColor(Color.TRANSPARENT);
                tv4.setBackgroundColor(Color.TRANSPARENT);
                tv5.setBackgroundColor(Color.TRANSPARENT);
                tv6.setBackgroundColor(Color.TRANSPARENT);
                tv7.setBackgroundColor(Color.TRANSPARENT);
                tv8.setBackgroundColor(Color.TRANSPARENT);
                tv9.setBackgroundColor(Color.RED);
                tv10.setBackgroundColor(Color.TRANSPARENT);
                break;
            case 9:
                tv1.setBackgroundColor(Color.TRANSPARENT);
                tv2.setBackgroundColor(Color.TRANSPARENT);
                tv3.setBackgroundColor(Color.TRANSPARENT);
                tv4.setBackgroundColor(Color.TRANSPARENT);
                tv5.setBackgroundColor(Color.TRANSPARENT);
                tv6.setBackgroundColor(Color.TRANSPARENT);
                tv7.setBackgroundColor(Color.TRANSPARENT);
                tv8.setBackgroundColor(Color.TRANSPARENT);
                tv9.setBackgroundColor(Color.TRANSPARENT);
                tv10.setBackgroundColor(Color.RED);
                break;
        }
    }
}
